import { FC, useEffect } from "react";
import { QuestionParagraphPropsType } from "./interface";
import { Checkbox, Form } from "antd";
import TextArea from "antd/es/input/TextArea";

const PropComponent: FC<QuestionParagraphPropsType> = (
  props: QuestionParagraphPropsType
) => {
  const { text, isCenter, disabled, onChange } = props;

  const [form] = Form.useForm();

  useEffect(() => {
    form.setFieldsValue({ text, isCenter });
  }, [text, isCenter, form]);

  function handleValuesChange() {
    onChange?.(form.getFieldsValue());
  }

  return (
    <Form
      layout={"vertical"}
      initialValues={{ text, isCenter }}
      form={form}
      onValuesChange={handleValuesChange}
      disabled={disabled}
    >
      <Form.Item
        label="标题内容"
        name="text"
        rules={[{ required: true, message: "请输入标题内容" }]}
      >
        <TextArea rows={4} />
      </Form.Item>

      <Form.Item label="是否居中" name="isCenter" valuePropName="checked">
        <Checkbox></Checkbox>
      </Form.Item>
    </Form>
  );
};

export default PropComponent;
